import vue from 'vue';
export default {
	validate(self, obj, rules) {
		this.self = self;
		this.obj = obj;
		for(let item of rules) {
			let rules = item.rule;

			let value = this.obj[item.name]
			if(item.emptyOrRequire && !this.obj[item.name]) {
				continue;
			} else {
				for(let item_ of rules) {
					if(!this[item_](item)) {
						return false;
					}
				}
			}
		}
		return true;
	},
	validateEmpty_(currentItem) {
		return this.validateEmpty(this.self, this.obj[currentItem.name], currentItem.label);
	},
	validateNum_(currentItem) {
		return this.validateNum(this.self, this.obj[currentItem.name], currentItem.label);
	},
	validateID_(currentItem) {
		return this.validateID(this.self, this.obj[currentItem.name], currentItem.label);
	},
	validateTel_(currentItem) {
		return this.validateTel(this.self, this.obj[currentItem.name]);
	},
	validateEmail_(currentItem) {
		return this.validateEmail(this.self, this.obj[currentItem.name]);
	},
	validateUpload_(currentItem) {
		return this.validateUpload(this.self, this.obj[currentItem.name], currentItem.label);
	},
	validateSelect_(currentItem) {
		return this.validateSelect(this.self, this.obj[currentItem.name], currentItem.label);
	},
	validateWebSite_(currentItem) {
		return this.validateWebSite(this.self, this.obj[currentItem.name]);
	},

	validateEmpty(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			self.$message(noticeMsg + '不能为空！');
			return false;
		}
		return true;
	},
	validateNum(self, fieldValue, noticeMsg) {
		let value = parseInt(fieldValue);
		if(typeof value != "number") {
			self.self.$message(noticeMsg + '需为数字！');
			return false;
		}
		return true;
	},
	validateUpload(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			self.$message('请上传' + noticeMsg + "！");
			return false;
		}
		return true;
	},
	validateSelect(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			self.$message('请选择' + noticeMsg + "！");
			return false;
		}
		return true;
	},
	validateImgSizeAndType(self, file, imgTypes, imgSize) {
		if(!imgTypes) {
			imgTypes = '.png|.jpg|.jpeg|.gif'
		}
		let imgType = file.name.substr(file.name.lastIndexOf('.') + 1);
		if(imgTypes.indexOf(imgType.toLowerCase()) == -1) {
			self.$message.error('只能上传' + imgTypes + '格式的文件!');
			return false;
		}
		if(!imgSize) {
			imgSize = 1;
		}
		if(file.size / 1024 / 1024 > imgSize) {
			self.$message.error('上传文件大小不能超过 ' + imgSize + 'MB!');
			return false;
		}
		return true;
	},
	validateTel(self, tel) {
		if(tel) {
			var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
			if(!myreg.test(tel)) {
				self.$message('电话号码格式不正确！');
				return false;
			}
		}
		return true;
	},
	validateEmail(self, email) {
		if(email) {
			let myreg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
			if(!myreg.test(email)) {
				self.$message('邮箱格式不正确！');
				return false;
			}
		}
		return true;
	},
	validateWebSite(self, website) {
		if(website) {
			let myreg = /^((https|http|ftp|rtsp|mms){0,1}(:\/\/){0,1})www\.(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;
			if(!myreg.test(website)) {
				self.$message('请填写正确网站地址！');
				return false;
			}
		}
		return true;
	},
	validateID(self, idCard) {
		if(idCard) {
			let myreg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
			if(!myreg.test(idCard)) {
				this.self.$message('身份证格式不正确！');
				return false;
			}
		}
		return true;
	}
}